package org.honeyweb.gsoc.services.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.honeyweb.gsoc.dao.UsersDao;
import org.honeyweb.gsoc.model.Organizations;
import org.honeyweb.gsoc.model.Users;
import org.honeyweb.gsoc.services.UsersManager;
import org.springframework.transaction.annotation.Transactional;

public class UsersManagerImpl implements UsersManager {
	
    private final Log logger = LogFactory.getLog(UsersManagerImpl.class);
    private UsersDao usersDao = null;

	/**
     * setter to allows spring to inject usersDao implementation
     * @param usersDao : object (implementation of UsersDao interface) to inject.
     */
    public void setUserDao(UsersDao userDao) {
        this.usersDao = userDao;
    }
    

    
    /**
     * {@inheritDoc}
     */
    public boolean checkLogin (String login, String password) {
        return usersDao.checkLogin(login, password);
    }

    /**
     * {@inheritDoc}
     */
    public Users changePassword(String login, String password) {
        Users user = usersDao.getUser(login);
        if (user != null) {
            user.setPassword(password);
        }
        return user;
    }
    
    /**
     * {@inheritDoc}
     */
    public Users getUser(String login) {
        return usersDao.getUser(login);
    }
    
    /**
     * {@inheritDoc}
     */
    public void addUser(String login, String password, String retypePassword, String firstName, String lastName, String mail) {
        Users user = new Users(login,password,retypePassword, firstName, lastName);
        logger.debug("user is:"+user);
        usersDao.addUser(user);
    }
    
    /**
     * {@inheritDoc}
     */
    public void updateUserOrganizationsId (int organizationsId,int id ){
    	usersDao.updateUserOrganizationsId(organizationsId,id);
    }
    
    /**
     * {@inheritDoc}
     */
    public Users getAdmin (Organizations organization){
    	return usersDao.getAdmin(organization.getAdminId());
    }
    
    /**
     * {@inheritDoc}
     */
    public void updateUser (String login, String firstName, String lastName, String mail,int id){
    	usersDao.updateUser(login,firstName,lastName,mail,id);
    }
    


}
